home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / video / kbd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.9 KB  |  150 lines

  1. /*
  2.  * Copyright (C) 1990-1992 Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. #include    <stdio.h>
  15. #include    <unistd.h>
  16. #include    <errno.h>
  17. #include    <sys/types.h>
  18. #if !defined(SVR4)
  19. #include    <sys/keyboard.h>
  20. #include    <sys/console.h>
  21. #endif
  22. #include    <sys/termio.h>
  23. #include    <sys/signal.h>
  24. #if defined(SVR4)
  25. #include    <sys/kd.h>
  26. #else
  27. #include    <sys/vtkd.h>
  28. #endif
  29.  
  30. #include    "video.h"
  31.  
  32.  
  33. STATIC struct termio    old_tty;
  34. STATIC struct termio    new_tty;
  35. STATIC char        strmap[STRTABLN];
  36. extern char        kbstrmap[];
  37. STATIC int        got_tty;
  38. STATIC int        kbd_fd;
  39. STATIC int        timeout;
  40.  
  41.  
  42. /*ARGSUSED*/
  43. STATIC void
  44. sigalarm(
  45.     int        sig
  46.     )
  47. {
  48.     (void) signal(SIGALRM, sigalarm);
  49.     timeout++;
  50. }
  51.  
  52. /*
  53.  * kbdInit()        - save initial tty mode
  54.  *              save initial function key string table
  55.  *              set keyboard into "raw" mode
  56.  *              install new function key string table
  57.  *              initialize the screen-switch handler
  58.  */
  59. void
  60. kbdInit()
  61. {
  62.     kbd_fd    = 0;
  63.  
  64.     if (ioctl(kbd_fd, TCGETA, &old_tty) != 0)
  65.     fatal(errno, "kbdInit() - can't get current tty mode");
  66.  
  67.     if (ioctl(kbd_fd, GIO_STRMAP, strmap) != 0)
  68.     fatal(errno, "kbdInit() - can't get function key string table");
  69.  
  70.     got_tty        = 1;
  71.     new_tty        = old_tty;
  72.     new_tty.c_iflag    &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IUCLC);
  73.     new_tty.c_lflag    &= ~(ICANON|ECHO);
  74.     new_tty.c_cc[VMIN]    = 1;
  75.     new_tty.c_cc[VTIME]    = 0;
  76.  
  77.     if (ioctl(kbd_fd, TCSETAW, &new_tty) != 0)
  78.     fatal(errno, "kbdInit() - can't set new tty mode");
  79.  
  80.     if (ioctl(kbd_fd, PIO_STRMAP, kbstrmap) != 0)
  81.     fatal(errno, "kbdInit() - can't set function key string table");
  82.  
  83.     (void) signal(SIGALRM, sigalarm);
  84. }
  85.  
  86. void
  87. kbdReset()
  88. {
  89.     if (got_tty)
  90.     {
  91.     got_tty = 0;
  92.  
  93.     if (ioctl(kbd_fd, TCSETAW, &old_tty) != 0)
  94.         fatal(errno, "kbdReset() - can't restore previous tty mode");
  95.  
  96.     if (ioctl(kbd_fd, PIO_STRMAP, strmap) != 0)
  97.         fatal(errno, "kbdReset() - can't restore previous string table");
  98.     }
  99. }
  100.  
  101. static int
  102. kbdRead()
  103. {
  104.     unsigned char    c;
  105.  
  106.     do
  107.     {
  108.     vidCheckScreenSwitch(0);
  109.     } while (read(kbd_fd, &c, 1) != 1 && timeout == 0);
  110.  
  111.     return timeout ? -1 : (int)c;
  112. }
  113.  
  114. int
  115. kbdGetKey(
  116.     int    delay
  117.     )
  118. {
  119.     int        k;
  120.     extern short    extkeymap[];
  121.  
  122.     if (delay == 0 && rdchk(kbd_fd) == 0)
  123.     {
  124.     vidCheckScreenSwitch(0);
  125.     return 0;
  126.     }
  127.  
  128.     timeout    = 0;
  129.     if (delay > 0)
  130.     alarm(delay);
  131.  
  132.     if ((k = kbdRead()) < 0)    /* got a timeout    */
  133.     return 0;
  134.  
  135.     if (delay > 0)
  136.     alarm(0);
  137.     timeout    = 0;
  138.  
  139.     return  (k == 0xff) ? extkeymap[kbdRead()] : k;
  140. }
  141.  
  142. void
  143. kbdBell()
  144. {
  145.  
  146.     ioctl(kbd_fd, KIOCSOUND, 2000);
  147.     nap(200);
  148.     ioctl(kbd_fd, KIOCSOUND, 0);
  149. }
  150.